﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace System.Web.Mvc
{
    public static class HtmlHelperEx
    {

        /// <summary>
        /// 主要就是输出分页的超级链接的标签
        ///自定义分页Helper扩展
        /// </summary>
        /// <param name="htmlHelper">htmlHelper</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="totalCount">总记录条数</param>
        /// <returns></returns>
        public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)
        {
            var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
            pageSize = pageSize == 0 ? 3 : pageSize;
            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
            var output = new StringBuilder();
            if (totalPages > 1)
            {
                // output.AppendFormat("<form action='{0}'>", redirectTo);
                //if (currentPage != 1)
                {//处理首页连接
                    output.AppendFormat("<ul><li><a  href='{0}?pageIndex=1'>首页</a></li> ", redirectTo, pageSize);
                }
                if (currentPage > 1)
                {//处理上一页的连接
                    output.AppendFormat("<li><a  href='{0}?pageIndex={1}'>上一页</a></li> ", redirectTo, currentPage - 1, pageSize);
                }
                else
                {
                    // output.Append("<span >上一页</span>");
                }

                output.Append(" ");

                if (totalPages >= 10)
                {
                    if (currentPage > totalPages - 5 && currentPage <= totalPages)
                    {
                        for (int n = 1; n < 5 - (totalPages - currentPage); n++)
                        {
                            output.AppendFormat("<li><a  href='{0}?pageIndex={1}'>{2}</a> </li>", redirectTo, totalPages - 10 + n, totalPages - 10 + n);
                        }
                        output.Append(" ");
                    }

                }
                int currint = 5;

                for (int i = 0; i < 10; i++)
                {//一共最多显示10个页码，前面5个，后面4个

                    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
                    {
                        if (currint == i)
                        {//当前页处理
                            //output.Append(string.Format("[{0}]", currentPage));
                            output.AppendFormat("<li><a class='cpb' href='javascript:viod(0)'>{0}</a></li> ", currentPage);
                        }
                        else
                        {//一般页处理
                            output.AppendFormat("<li><a  href='{0}?pageIndex={1}'>{2}</a> </li>", redirectTo, currentPage + i - currint, currentPage + i - currint);
                        }
                        output.Append(" ");
                    }

                }
                if (totalPages >= 10)
                {
                    if (currentPage >= 1 && currentPage <= 5)
                    {
                        for (int n = 0; n < 5 - currentPage + 1; n++)
                        {
                            output.AppendFormat("<li><a  href='{0}?pageIndex={1}'>{2}</a></li> ", redirectTo, currentPage + 5 + n, currentPage + 5 + n);
                        }
                        output.Append(" ");
                    }
                }

                if (currentPage < totalPages)
                {//处理下一页的链接
                    output.AppendFormat("<li><a  href='{0}?pageIndex={1}'>下一页</a></li> ", redirectTo, currentPage + 1);
                }
                else
                {
                    //output.Append("<span >下一页</span>");
                }
                output.Append(" ");
                if (currentPage != totalPages)
                {
                    output.AppendFormat("<li><a  href='{0}?pageIndex={1}'>末页</a><li> ", redirectTo, totalPages);
                }
                output.Append(" ");
            }
            //   output.AppendFormat("<span class=\"text\">第<span class=\"text\" id=\"currPage\">{0}</span>页 / 共<span class=\"text\" id=\"totalPage\">{1}</span>页</span>", currentPage, totalPages);//这个统计加不加都行
            //   output.AppendFormat(" ");
            //   output.AppendFormat("<span class=\"text\">到第</span>");
            //output.AppendFormat("<input class=\"num\" id=\"goToPage\" name=\"pageIndex\" value=\"{0}\"  >",currentPage);
            //output.AppendFormat("<span class=\"text\">页</span>");
            //output.AppendFormat("<button class=\"submit\" type=\"button\" id=\"ToPage\" onclick=\"goToPageX('currPage','totalPage','goToPage','{0}');\">确定</button>", redirectTo);
            output.AppendFormat("</ul>");
            //output.Append("</form>");

            return new HtmlString(output.ToString());
        }

        /// <summary>
        /// 自定义分页Helper扩展 下拉框版
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public static HtmlString ShowPageNavigateXiaLa(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)
        {
            string query=htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.Query;
            var redirectTo="";
            if (string.IsNullOrEmpty(query))
            {
               redirectTo  = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath+"?";
            }
            else
            {
                int m=query.IndexOf("pageIndex");
                if (m>0)//有pageIndex参数
                {
                    string laji = query.Substring(m, 11);
                    query=query.Replace(laji,"");
                }
                redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath+query+"&";
            }
            
            pageSize = pageSize == 0 ? 3 : pageSize;
            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
            var output = new StringBuilder();
            if (totalPages > 0)
            {
                output.AppendFormat("<div class='page-text'>");
                if (currentPage!=totalPages)
                {
                    output.AppendFormat("<a href='{0}pageIndex={1}' class='pop-btn next'>下一页</a> ", redirectTo, currentPage + 1);
                }

                output.AppendFormat("<select class='select' onchange='GotoPage(this);' >");
                //处理首页连接
                

                output.Append(" ");

                if (totalPages >= 10)
                {
                    if (currentPage > totalPages - 5 && currentPage <= totalPages)
                    {
                        for (int n = 1; n < 5 - (totalPages - currentPage); n++)
                        {
                            output.AppendFormat("<option value='{0}pageIndex={1}'>第{2}页</option>", redirectTo, totalPages - 10 + n, totalPages - 10 + n);
                        }
                        output.Append(" ");
                    }

                }
                int currint = 5;

                for (int i = 0; i < 10; i++)
                {//一共最多显示10个页码，前面5个，后面4个

                    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
                    {
                        if (currint == i)
                        {//当前页处理
                            
                            output.AppendFormat("<option selected='selected' value='javascript:viod(0)'>第{0}页</option> ", currentPage);
                        }
                        else
                        {//一般页处理
                            output.AppendFormat("<option value='{0}pageIndex={1}'>第{2}页</option>", redirectTo, currentPage + i - currint, currentPage + i - currint);
                        }
                        output.Append(" ");
                    }

                }
                if (totalPages >= 10)
                {
                    if (currentPage >= 1 && currentPage <= 5)
                    {
                        for (int n = 0; n < 5 - currentPage + 1; n++)
                        {
                            output.AppendFormat("<option value='{0}pageIndex={1}'>第{2}页</option> ", redirectTo, currentPage + 5 + n, currentPage + 5 + n);
                        }
                        output.Append(" ");
                    }
                }

                output.Append(" ");
                //if (currentPage != totalPages)
                //{
                //    output.AppendFormat("<option value='{0}pageIndex={1}'>末页</option> ", redirectTo, totalPages);
                //}
               
                output.Append(" ");
            }
           
            output.AppendFormat("</select>");
            if (currentPage != 1)
            {
                output.AppendFormat("<a href='{0}pageIndex={1}' class='pop-btn prev'>上一页</a> ", redirectTo, currentPage - 1);
            }
            output.AppendFormat("</div>");

            output.AppendFormat("<script type='text/javascript'> function GotoPage(obj) {{location.href = $(obj).val();   }};</script>");
            return new HtmlString(output.ToString());
        }


        /// <summary>
        /// 在网页弹出message信息
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="message">要弹出的内容</param>
        /// <returns></returns>
        public static HtmlString ShowMessage_UI(this HtmlHelper htmlHelper, string message)
        {
            if (!string.IsNullOrEmpty(message))
            {
                var output = new StringBuilder();

                output.AppendFormat("<div id='dialog-message' title='Message'> <p id='p_message_id'> </p> </div>");

                output.AppendFormat("<script type='text/javascript'> $(function () {{  $('#p_message_id').html('{0}');  $('#dialog-message').dialog({{   buttons: {{   Ok: function () {{   $(this).dialog('close');   }}  }}   }});  }});  </script>", message);


                return new HtmlString(output.ToString());

            }
            else
            {
                return null;
            }

        }

    }
}